//  数字的处理

/*
* @description 小数保留，传入指定位数，返回目标位数的小数
* @param    total：传入number
*           num：截取小数位数
* @return   number
*/
function ToolPoint(total, num) {
    if (!total.toString()) {
        return total;
    }
    total = total.toString();
    if (total.indexOf(".") != -1) {
        let point = total.split(".")
        if (point[1].length >= num) {
            total = point[0] + "." + point[1].substring(0, num);
            return total * 1
        } else {
            return total * 1;
        }
    } else {
        return total * 1;
    }
}
/*
*@description保留固定的位数，不够补零
*@param value,length
*/
function PointFix(num, length) {
    if (!num.toString()) {
        return num;
    }
    length = length ? length : 4;
    let number = transNum(num);
    if (number.indexOf(".") != -1) {
        let point = number.split(".");
        if (point[1].length >= length) {
            // 截取
            point = point[0] + "." + point[1].substring(0, length);
            return point.toString();
        } else {
            // 补0
            number = (number * 1).toFixed(length + 1).slice(0, -1);
            return number.toString();
        }
    } else {
        number = (number * 1).toFixed(length + 1).slice(0, -1)
        return number.toString();
    }
}

/*
* @description 科学计数法处理
* @param       number
* @return      number
*/
function TransNum(scientific_number) {
    scientific_number = scientific_number.toString();
    if (scientific_number.indexOf('+') != -1) {
        scientific_number = scientific_number.replace('+', '')
    }
    if ((scientific_number.indexOf('E') != -1) || (scientific_number.indexOf('e') != -1)) {
        var resValue = '',
            power = '',
            result = null,
            dotIndex = 0,
            resArr = [],
            sym = '';
        var numStr = scientific_number.toString();
        if (numStr[0] == '-') { // 如果为负数，转成正数处理，先去掉‘-’号，并保存‘-’.
            numStr = numStr.substr(1);
            sym = '-';
        }
        if ((numStr.indexOf('E') != -1) || (numStr.indexOf('e') != -1)) {
            var regExp = new RegExp('^(((\\d+.?\\d+)|(\\d+))[Ee]{1}((-(\\d+))|(\\d+)))$', 'ig');
            result = regExp.exec(numStr);
            if (result != null) {
                resValue = result[2];
                power = result[5];
                result = null;
            }
            if (!resValue && !power) { return false }
            dotIndex = resValue.indexOf('.') == -1 ? 0 : resValue.indexOf('.');
            resValue = resValue.replace('.', '');
            resArr = resValue.split('');
            if (Number(power) >= 0) {
                var subres = resValue.substr(dotIndex);
                power = Number(power);
                //幂数大于小数点后面的数字位数时，后面加0
                for (var i = 0; i <= power - subres.length; i++) {
                    resArr.push('0');
                }
                if (power - subres.length < 0) {
                    resArr.splice(dotIndex + power, 0, '.');
                }
            } else {
                power = power.replace('-', '');
                power = Number(power);
                //幂数大于等于 小数点的index位置, 前面加0
                for (var i = 0; i < power - dotIndex; i++) {
                    resArr.unshift('0');
                }
                var n = power - dotIndex >= 0 ? 1 : -(power - dotIndex);
                resArr.splice(n, 0, '.');
            }
        }
        resValue = resArr.join('');

        return sym + resValue;
    } else {
        return scientific_number
    }

}

export default {
    ToolPoint,
    PointFix,
    TransNum,

}